package com.itheima.info.manager.system.controller;

import com.itheima.info.manager.system.domain.Student;
import com.itheima.info.manager.system.factory.Factory;
import com.itheima.info.manager.system.service.IStudentService;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * 学生交互 实现类
 */
public class StudentControllerImpl implements IStudentController {
    private Scanner sc = new Scanner(System.in);
    private IStudentService studentService = Factory.getStudentService();

    @Override
    public void addStudent() {
        String id;
        while (true) {
            System.out.println("请输入学号");
            id = sc.next();
            boolean exits = studentService.isExits(id);
            if (exits) {
                System.out.println("已经存在,请重新输入");
            } else {
                break;
            }
        }
        Student student = getStudentInfo(id);
        studentService.addStudent(student);
        System.out.println("添加成功");
    }


    @Override
    public void deleteStudent() {
        String id = getId();
        studentService.deleteStudent(id);
    }


    @Override
    public void updateStudent() {
        String id = getId();
        Student student = getStudentInfo(id);
        studentService.updateStudent(id,student);

    }

    @Override
    public void findAllStudent() {
        ArrayList<Student> allStudent = studentService.findAllStudent();
        System.out.println(allStudent);
    }

    @Override
    public void start() {

        //
        while (true) {
            System.out.println("--------欢迎来到黑马信息管理系统--------");
            System.out.println("请输入您的选择: 1.增  2.删 3.改 4.查 5.退出");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    deleteStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    findAllStudent();
                    break;
                case 5:
                    System.out.println("回到主界面");
                    return;
                default:
                    System.out.println("瞪大你的狗眼看看你输得啥!");
                    break;
            }
        }
    }

    private Student getStudentInfo(String id) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入班级");
        String banji = sc.next();
        return new Student(id, name, age, banji);
    }

    private String getId() {
        String id;
        while (true) {
            System.out.println("请输入学号");
            id = sc.next();
            boolean exits = studentService.isExits(id);
            if (exits) {
                break;
            } else {
                System.out.println("不存在,请重新输入");
            }
        }
        return id;
    }

}
